/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Services;

import au.com.bytecode.opencsv.CSVReader;
import com.ControlEscolar.Beans.Exam;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.ExamDAO;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Exceptions.MaxExamAssigned;
import com.ControlEscolar.Exceptions.NotAceptExamRequestExp;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Validations.ValidationExam;
import com.ControlEscolar.Validations.ValidationExamRequest;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;



/**
 *
 * @author Rodna Marrufo
 */
public class ExamService {

 ValidationExam validationExam=new ValidationExam();
 ExamDAO examDAO=new ExamDAO();
 ValidationExamRequest validationExamRequest= new ValidationExamRequest();

 public void addExam(Exam exam) throws AlreadyExistExp, NotAceptExamRequestExp, MaxExamAssigned, ServiceExp{
        try {
            String subjectKey = exam.getSubjectKey();
            String studentKey = exam.getStudentKey();
            final String EXTRAORDINARY = "EX";
            final String ORDINARY = "OR";
            if (validationExamRequest.isExamRequestAccepted(exam.getID_Request())) {
                if (exam.getExamType().compareTo(ORDINARY) == 0) {
                    if (validationExam.isMaxOrdinaryAssigned(subjectKey, studentKey))
                        throw new MaxExamAssigned("Maximo de Examenes Asignados");
                } else {
                    if (exam.getExamType().compareTo(EXTRAORDINARY) == 0) {
                        if (validationExam.isMaxExtraAssigned(subjectKey, studentKey))
                            throw new MaxExamAssigned("Maximo de Examenes Asignados");
                    }
                }
                examDAO.add(exam);
            }
            else throw new NotAceptExamRequestExp("No se acepto la petición del Examen");
            
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

    
     public ArrayList<Exam> getStudentLastExams(String studentKey) throws ServiceExp{
        try {
            return examDAO.queryLastExams(studentKey);
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
     }


    public void updateGrade(Exam exam) throws ServiceExp{
        try {
             examDAO.update(exam);
        } catch (DAOException ex) {
           throw new ServiceExp();
        }
    }

    //no se utiliza por el momento
    protected Exam getLastExam(String subjectKey, String studentKey) throws DAOException{
        return examDAO.queryLastExam(subjectKey, studentKey);
    }

    //no se utiliza por el momento
    public ArrayList<Exam> getStudentGrades(String studentKey) throws ServiceExp{
       try{
            return examDAO.queryStudentExams(studentKey);
       }
       catch(DAOException e){
         throw new ServiceExp();

   }}

    public Exam getExam(String examKey) {
        return examDAO.queryByID(examKey);
    }

    public ArrayList<Exam> getExamList() throws ServiceExp{
        try {
            return examDAO.query();
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

     public void readFileExam(String ADRESS_FILE) throws ServiceExp, AlreadyExistExp, NotAceptExamRequestExp, MaxExamAssigned{
        try {
            CSVReader reader = new CSVReader(new FileReader(ADRESS_FILE));
            List<String[]> examInfo = reader.readAll();
            
            Exam exam = new Exam();
            for (int index = 1; index < examInfo.size(); index++) {
                exam.setID_Professor(Integer.parseInt(examInfo.get(index)[0]));
                exam.setStudentKey(examInfo.get(index)[1]);
                exam.setSubjectKey(examInfo.get(index)[2]);
                exam.setExamType(examInfo.get(index)[3]);
                exam.setExamGrade(Float.parseFloat(examInfo.get(index)[4]));
                exam.setDates(Date.valueOf(examInfo.get(index)[5]));
                exam.setID_Request(Long.parseLong(examInfo.get(index)[6]));
                addExam(exam);
            }
        } catch (AlreadyExistExp ex) {
           throw new AlreadyExistExp("Ya existe dato");
        } catch (NotAceptExamRequestExp ex) {
            throw new NotAceptExamRequestExp("No fue Aceptada la Solicitud");
        } catch (MaxExamAssigned ex) {
            throw new MaxExamAssigned("Numero Maximo de Materias Asignadas");
        } catch (ServiceExp ex) {
           throw new ServiceExp();
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new ServiceExp();
        }

        }

     public static void main(String[] args) throws AlreadyExistExp, NotAceptExamRequestExp, MaxExamAssigned{
        try {
            ExamService service = new ExamService();
            service.readFileExam("ExamGrades.csv");
        } catch (ServiceExp ex) {
            ex.printStackTrace();
        }
     }
}
